﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// 时间线(多用于技能)
public class TimeLine
{
    public float TimeSpeed { get; set; }

    //重置事件
    private Action _resetCallback;
    // 开始事件
    private Action _startCallback;

    public TimeLine()
    {
        TimeSpeed = 1;

        Reset();
    }

    /// <summary>
    /// 添加事件
    /// id:穿透参数
    /// </summary>
    public void AddEvent(float delay, int id, Action<int> method)
    {
        LineEvent param = new LineEvent(delay, id, method);
        _startCallback += param.Start;
        _resetCallback += param.Stop;
    }
    public void AddEvent(float delay, object obj, Action<object> method)
    {
        LineEvent param = new LineEvent(delay, obj, method);
        _startCallback += param.Start;
        _resetCallback += param.Stop;
    }
    public void Start()
    {
        Reset();

        if (null != _startCallback)
        {
            _startCallback();
        }
    }
    public void Reset()
    {
        if (null != _resetCallback)
        {
            _resetCallback();
        }
    }

    /// <summary>
    /// 时间线事件
    /// </summary>
    private class LineEvent
    {
        Timer _timer; // 时间线的驱动
        public LineEvent(float delay, int id, Action<int> method)
        {
            _timer = TimerMgr.instance.CreateTimer(delay, 1, ()=> { if (method != null) { method(id); } });
        }
        public LineEvent(float delay, object obj, Action<object> method)
        {
            _timer = TimerMgr.instance.CreateTimer(delay, 1, () => { if (method != null) { method(obj); } });
        }

        public void Stop()
        {
            _timer.Stop();
        }

        public void Start()
        {
            _timer.Start();
        }
    }
}
